<?php

/**
 * Sidebar filter system
 * 
 * @project : Loobica
 * @author  : Dino
 * @date    : 15 Mar 2014
 * 
 */
class Filter_system {

  var $filter_input = array();
  var $filter_data = array();
  var $filter_category = null;

  public function __construct() {
    $this->ci = & get_instance();
  }

  /**
   * Initilize filter system
   * 
   */
  public function init($form_path = null) {
    $view_data = array();
    $view_data['filter_enabled'] = true;
    $view_data['filter_form_path'] = ($form_path) ? $form_path : current_url();
    return array_merge($view_data, $this->input(), $this->data());
  }

  /**
   * Set category
   * 
   * @param type $category_id
   * @return \Filter_system
   */
  public function category($category_id = 0) {
    $this->filter_category = (!empty($category_id) && is_numeric($category_id)) ? $category_id : null;
    return $this;
  }

  /**
   * Get inputs for the filtering system
   * 
   * @return \Filter_system
   */
  public function input() {

    // Clearing inputs array
    $this->filter_input = array();

    // Ads Sort by
    $sort_by_options = array('title', 'price', 'date', 'views');
    $this->filter_input['filter_sort_by'] = (in_array($this->ci->input->get('sort_by'), $sort_by_options)) ? $this->ci->input->get('sort_by') : 'date';

    // Ads Sort order
    $sort_order_options = array('asc', 'desc');
    $this->filter_input['filter_sort_order'] = (in_array($this->ci->input->get('sort_order'), $sort_order_options)) ? $this->ci->input->get('sort_order') : 'desc';

    // Ad items
    $items = $this->ci->input->get('items');
    $this->filter_input['filter_items_sel'] = (!empty($items) && is_array($items)) ? $items : array();

    // Ad item types
    $item_types = $this->ci->input->get('item_types');
    $this->filter_input['filter_item_types_sel'] = (!empty($item_types) && is_array($item_types)) ? $item_types : array();

    // Ad item extras
    $item_extras = $this->ci->input->get('item_extras');
    $this->filter_input['filter_item_extras_sel'] = (!empty($item_extras) && is_array($item_extras)) ? $item_extras : array();

    // Ad brands
    $brands = $this->ci->input->get('brands');
    $this->filter_input['filter_brands_sel'] = (!empty($brands) && is_array($brands)) ? $brands : array();

    // Ad brand models
    $brand_models = $this->ci->input->get('brand_models');
    $this->filter_input['filter_brand_models_sel'] = (!empty($brand_models) && is_array($brand_models)) ? $brand_models : array();

    // Ad price ranges
    $price_ranges = $this->ci->input->get('price_ranges');
    $this->filter_input['filter_price_ranges_sel'] = (!empty($price_ranges) && is_array($price_ranges)) ? $price_ranges : array();

    // Ad item condition
    $condition = $this->ci->input->get('condition');
    $this->filter_input['filter_condition_sel'] = (!empty($condition) && is_array($condition)) ? $condition : array();

    // Ad posted by
    $posted_by = $this->ci->input->get('posted_by');
    $this->filter_input['filter_posted_by_sel'] = (!empty($posted_by) && is_array($posted_by)) ? $posted_by : array();

    // Ad give for
    $give_for = $this->ci->input->get('give_for');
    $this->filter_input['filter_give_for_sel'] = (!empty($give_for) && is_array($give_for)) ? $give_for : array();

    // Ad people
    $peoples = $this->ci->input->get('peoples');
    $this->filter_input['filter_peoples_sel'] = (!empty($peoples) && is_array($peoples)) ? $peoples : array();

    return $this->filter_input;
  }

  /**
   * Filter system data
   * 
   * @return \Filter_system
   */
  public function data() {

    // Clearing inputs array
    $this->filter_data = array();

    // Ad items
    $this->filter_data['filter_items'] = $this->ci->filter_model->get_items($this->filter_category);

    // Ad item types
    $this->filter_data['filter_item_types'] = $this->ci->filter_model->get_item_types($this->filter_category);

    // Ad item extras
    $this->filter_data['filter_item_extras'] = $this->ci->filter_model->get_item_extras($this->filter_input['filter_items_sel']);

    // Ad item brands
    $this->filter_data['filter_brands'] = $this->ci->filter_model->get_brands($this->filter_category);

    // Ad items
    $this->filter_data['filter_brand_models'] = $this->ci->filter_model->get_brand_models($this->filter_category);

    // Price ranges
    $this->filter_data['filter_price_ranges'] = $this->ci->filter_model->get_price_ranges();

    // Price ranges
    $this->filter_data['filter_give_for'] = $this->ci->filter_model->get_give_for();

    // Price ranges
    $this->filter_data['filter_peoples'] = $this->ci->filter_model->get_peoples();

    return $this->filter_data;
  }

}

?>